﻿using Game.NetWork.Packages;
using Game.NetWork;
using Game.Server.Creator;
using Game.Server.PlayerEvent;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Engine;

namespace Game.Server.Plugins
{
    public class BlockRecordPlugin : ServerPlugin, IPlayerBreakAndPlaceHandle
    {
        public byte FirstLevel => 0;

        private string m_recordMessage = "";
        public override int Version => 10000;

        public override string Name => "方块记录";

        public static string BlockRecordPath;

        public override void Initialize()
        {
            PlayerBreakAndPlaceBlockEventManager.AddObject(this);
        }

        public override void Save()
        {
            if (m_recordMessage == string.Empty)
            {
                return;
            }
            using (FileStream fileStream = File.Open(BlockRecordPath, FileMode.OpenOrCreate))
            {
                fileStream.Position = fileStream.Length;
                using (StreamWriter streamWriter = new StreamWriter(fileStream, Encoding.UTF8))
                {
                    streamWriter.WriteLine(m_recordMessage);
                }
            }
            m_recordMessage = string.Empty;
        }

        public override void Load()
        {
            BlockRecordPath = GetPluginDataByWorldPath("BlockRecord.txt");
        }

        public string GetPluginDataByWorldPath(string fileName)
        {
            string pluginDataPath = Storage.GetSystemPath("app:/PluginData");
            if (!Directory.Exists(pluginDataPath))
            {
                Directory.CreateDirectory(pluginDataPath);
            }
            string worldName = GameManager.Project.FindSubsystem<SubsystemGameInfo>().WorldSettings.Name;
            string dataByWorldPath = Storage.CombinePaths(pluginDataPath, worldName);
            if (!Directory.Exists(dataByWorldPath))
            {
                Directory.CreateDirectory(dataByWorldPath);
            }
            return Storage.CombinePaths(dataByWorldPath, fileName);
        }

        public bool PlayerBreakEvent(ComponentPlayer componentPlayer, Point3 point, int digBlockValue, int toolLevel)
        {
            void WriteMessage(string message)
            {
                string mes = $"{DateTime.Now.ToString("HH:mm:ss.fff")} {componentPlayer.PlayerData.Name} IP:{componentPlayer.PlayerData.Client.Peer.EndPoint} 社区ID:{componentPlayer.PlayerData.Client.CommunityAccountId} GUID:{componentPlayer.PlayerData.Client.GUID} ITEMID:{digBlockValue}  " + message;
                m_recordMessage += mes + "\n";
                Log.Information(mes);
            }
            switch (digBlockValue)
            {
                case 0:
                    WriteMessage($"尝试破坏空气在 {point.X},{point.Y},{point.Z}");
                    return false;
                case 1:
                    WriteMessage($"尝试破坏基岩在 {point.X},{point.Y},{point.Z}");
                    return false;
            }
            return true;
        }

        public bool PlayerPlaceEvent(ComponentPlayer componentPlayer, Point3 point, int placeBlockValue)
        {
            void WriteMessage(string message)
            {
                string mes = $"{DateTime.Now.ToString("HH:mm:ss.fff")} {componentPlayer.PlayerData.Name} IP:{componentPlayer.PlayerData.Client.Peer.EndPoint} GUID:{componentPlayer.PlayerData.Client.GUID}  ITEMID:{placeBlockValue} " + message;
                m_recordMessage += mes + "\n";
                Log.Information(mes);
            }

            // 虚空监狱
            void CreateCage()
            {
                int boxSize = 3;
                Point3 point3 = new Point3(componentPlayer.ComponentBody.Position);
                Point3 startPoint3 = new Point3(point3.X + boxSize, point3.Y + boxSize, point3.Z + boxSize);
                Point3 stopPoint3 = new Point3(point3.X - boxSize, point3.Y - boxSize, point3.Z - boxSize);
                foreach (Point3 p in CreatorGenerationAlgorithm.Rectangular(startPoint3, stopPoint3, true))
                {
                    IPackage package = new SubsystemTerrainPackage(p.X, p.Y, p.Z, 1);
                    package.To = componentPlayer.PlayerData.Client;
                    CommonLib.Net.QueuePackage(package);
                }
            }

            int content = Terrain.ExtractContents(placeBlockValue);
            int light = Terrain.ExtractLight(placeBlockValue);
            int data = Terrain.ExtractData(placeBlockValue);

            switch (content)
            {
                case 0:
                    WriteMessage($"尝试放置空气？在 {point.X},{point.Y},{point.Z}");
                    // CreateCage();
                    return false;
                case 1:
                    if (data == 0 && light == 0)
                    {
                        WriteMessage($"尝试放置基岩在 {point.X},{point.Y},{point.Z}");
                        // 放基岩？那就给他建个虚空监狱
                        CreateCage();
                        return false;
                    }
                    else if (data == 1)
                    {
                        WriteMessage($"尝试放置领地石 ${placeBlockValue} 在 {point.X},{point.Y},{point.Z}");
                        return true;
                    }
                    WriteMessage($"尝试放置基岩id的方块 ${placeBlockValue} 在 {point.X},{point.Y},{point.Z}");
                    return true;
                case 105:
                case 106:
                case 107:
                case 234:
                case 235:
                case 236:
                    WriteMessage($"放置炸药桶在 {point.X},{point.Y},{point.Z}");
                    return true;
                case 216:
                    WriteMessage($"尝试放置发射器在 {point.X},{point.Y},{point.Z}");
                    return true;
                case 237:
                    WriteMessage($"尝试放置活塞在 {point.X},{point.Y},{point.Z}");
                    return true;
            }
            return true;
        }



    }
}